<?php

namespace TpAmis\Renderers;


class BaseRenderer implements \JsonSerializable
{
    // use Macroable {
    //     __call as macroCall;
    // }

    public $type;

    public $amisSchema = [];

    public static function make()
    {
        return new static();
    }

    public function __call($method, $parameters)
    {

        return $this->set($method, array_shift($parameters));
    }

    public function set($name, $value)
    {
        if ($name == 'map' && is_array($value) && array_keys($value) == array_keys(array_keys($value))) {
            $value = (object) $value;
        }

        $this->amisSchema[$name] = $value;

        return $this;
    }

    public function get($name)
    {
        return $this->amisSchema[$name];
    }
    public function jsonSerialize(): array
    {
        return $this->filteredResults();
    }

    public function toJson()
    {
        return json_encode($this->amisSchema);
    }

    public function toArray()
    {
        return $this->amisSchema;
    }

    /**
     * @param string $sign         权限标识
     * @param mixed  $replaceValue 无权限时替换的值
     *
     * @return $this
     */
    public function permission(string $sign, $replaceValue = '')
    {
        $this->amisSchema['owl_permission'] = $sign;
        $this->amisSchema['owl_permission_replace_value'] = $replaceValue;

        return $this;
    }

    public function filteredResults(): array
    {
        return $this->amisSchema;
    }
}
